package com.xingfudazi.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingfudazi.app.domain.AppFollowRel;
import com.xingfudazi.app.dto.MemberAvatar;
import com.xingfudazi.app.mapper.AppFollowRelMapper;
import com.xingfudazi.app.service.IAppFollowRelService;
import com.xingfudazi.app.service.IAppMemberService;
import com.xingfudazi.app.service.ImFriendService;
import com.xingfudazi.common.exception.ServiceException;
import com.xingfudazi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author YuJiaMing
 * date 2024年07月22日 15:03
 */
@Service
public class AppFollowRelServiceImpl extends ServiceImpl<AppFollowRelMapper, AppFollowRel> implements IAppFollowRelService {

    @Autowired
    private IAppMemberService appMemberService;

    @Autowired
    private ImFriendService imFriendService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(AppFollowRel appFollowRel) {
        Long nid = SecurityUtils.getUserId();
        Long followId = appFollowRel.getFollowId();

        // 是否重复
        LambdaQueryWrapper<AppFollowRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppFollowRel::getNid, nid);
        wrapper.eq(AppFollowRel::getFollowId, followId);
        if (this.count(wrapper) > 0) {
            throw new ServiceException("请勿重复关注");
        }

        appFollowRel.setId(null);
        appFollowRel.setCreateTime(LocalDateTime.now());
        appFollowRel.setNid(nid);
        this.save(appFollowRel);

        // 账号中添加关注记录
        this.appMemberService.follow(followId, 1);

        imFriendService.addByAppFollowRel(appFollowRel);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Long followId) {
        if (followId == null) {
            return;
        }

        Long nid = SecurityUtils.getUserId();
        LambdaQueryWrapper<AppFollowRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppFollowRel::getNid, nid);
        wrapper.eq(AppFollowRel::getFollowId, followId);
        AppFollowRel appFollowRel = this.getOne(wrapper);
        if (appFollowRel == null) {
            return;
        }
        this.removeById(appFollowRel.getId());
        this.appMemberService.follow(followId, -1);

        imFriendService.delByAppFollowRel(followId, SecurityUtils.getUserId());
    }


    @Override
    public List<MemberAvatar> dogTurn(Integer pageSize, Long lastId, String search, Long nid) {
        return this.baseMapper.dogTurn(pageSize, lastId, search, nid);
    }

    @Override
    public List<MemberAvatar> loveTurn(Integer pageSize, Long lastId, String search, Long nid) {
        return this.baseMapper.loveTurn(pageSize, lastId, search, nid);
    }

    @Override
    public int dogTurnCount(Long userId) {
        LambdaQueryWrapper<AppFollowRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppFollowRel::getFollowId, userId);
        return this.count(wrapper);
    }

    @Override
    public Long loveTurnCount(Long userId) {
        return this.baseMapper.loveTurnCount(userId);
    }

    @Override
    public Set<Long> getFollowUserIds(Long userId, List<Long> nids) {
        LambdaQueryWrapper<AppFollowRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppFollowRel::getNid,userId);
        wrapper.in(AppFollowRel::getFollowId,nids);
        wrapper.select(AppFollowRel::getFollowId);
        return this.list(wrapper).stream().map(AppFollowRel::getFollowId).collect(Collectors.toSet());
    }
}
